package com.baox.agridata.synchronous.util;

import org.springframework.util.DigestUtils;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.*;

public class CommonUtil {

    public static boolean notEmpty(List ls){
        return  ls!=null && ls.size()>0;
    }

    public static boolean empty(List ls){
        return  ls==null || ls.size()==0;
    }



    //MD5加密
    public static String md5(String key){
        if(key != null && key.length() > 0){
            return DigestUtils.md5DigestAsHex(key.getBytes());
        } else {
            return null;
        }
    }

    //生成随机字符串
    public static String generateUUID(){
        return UUID.randomUUID().toString().replaceAll("-","").toLowerCase();
    }

    /**
     * 比较两个实体属性值，返回一个map以有差异的属性名为key，value为一个list分别存obj1,obj2此属性名的值
     * @param obj1 进行属性比较的对象1
     * @param obj2 进行属性比较的对象2
     * @param compareArr 选择要比较的属性数组
     * @return 属性差异比较结果map
     */
    public static Map<String, Map> compareFields(Object obj1, Object obj2, String[] compareArr) {
        try {
            //装返回值得
            Map<String, Map> map = new LinkedHashMap<>();
            //需要对比的字段list
            List<String> ignoreList = null;
            if (compareArr != null && compareArr.length > 0) {
                // array转化为list
                ignoreList = Arrays.asList(compareArr);
            }
            // 只有两个对象都是同一类型的才有可比性
            if (obj1.getClass() == obj2.getClass()) {
                Class clazz = obj1.getClass();
                // 获取object的属性描述
                PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,
                        Object.class).getPropertyDescriptors();
                // 这里就是所有的属性了
                for (PropertyDescriptor pd : pds) {
                    // 属性名
                    String name = pd.getName();
                    // 如果当前属性选择进行比较，跳到下一次循环
                    if (ignoreList != null && ignoreList.contains(name)) {
                        // get方法
                        Method readMethod = pd.getReadMethod();
                        // 在obj1上调用get方法等同于获得obj1的属性值
                        Object objBefore = readMethod.invoke(obj1);
                        // 在obj2上调用get方法等同于获得obj2的属性值
                        Object objAfter = readMethod.invoke(obj2);
                        if (objBefore instanceof Timestamp) {
                            objBefore = new Date(((Timestamp) objBefore).getTime());
                        }
                        if (objAfter instanceof Timestamp) {
                            objAfter = new Date(((Timestamp) objAfter).getTime());
                        }
                        if (objBefore == null && objAfter == null) {
                            continue;
                        } else if (objBefore == null && objAfter != null) {
                            Map m = new LinkedHashMap();
                            m.put("objBefore",objBefore);
                            m.put("objAfter",objAfter);
                            map.put(name, m);
                            continue;
                        } else if( "".equals(objBefore) && objAfter ==null){
                            continue;
                        }
                        // 比较这两个值是否相等,不等则放入map
                        if (!objBefore.equals(objAfter)) {
                            Map m = new LinkedHashMap();
                            m.put("objBefore",objBefore);
                            m.put("objAfter",objAfter);
                            map.put(name, m);
                        }
                    }
                }
            }else {
                System.out.println("对象类型不一致，不能完成对比");
            }
            return map;
        } catch (Exception e) {
            System.out.println("错误");
            return null;
        }
    }


    public static void test(Object obj1, String[] compareArr) throws IntrospectionException {
        //需要对比的字段list
        List<String> ignoreList = null;
        if (compareArr != null && compareArr.length > 0) {
            // array转化为list
            ignoreList = Arrays.asList(compareArr);
        }
        Class clazz = obj1.getClass();
        // 获取object的属性描述
        PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,
                Object.class).getPropertyDescriptors();
        for (PropertyDescriptor pd : pds) {
            // 属性名
            String name = pd.getName();
            // 如果当前属性选择进行比较，跳到下一次循环
            if (ignoreList != null && ignoreList.contains(name)) {
                System.out.println("------" + name);
            }
        }

    }

    private static String[] SyrysComparFileds = new String[]{"zt1","zt2","zt3","zt4","zt5","zt9","Bh","Sfzh","Xm","Xb","Dh","Qhdm","Xl","Zc","Jglx","Jgid","Jgmc","Rq"};









}
